import json
from datetime import datetime

from django.http import JsonResponse
from django.views.generic import View

# Create your views here.
from apps.spit.models import Spit


class SpitView(View):
    def get(self, request):
        # 获取一级吐槽数据
        spits = Spit.objects.filter(parent=None).order_by('-publishtime')
        # 根据传递分页数据进行响应分页数据获取
        data_list = []
        for spit in spits:
            spit_dict = {
                'id': spit.id,
                'content': spit.content,
                'publishtime': spit.publishtime,
                'userid': spit.userid,
                'nickname': spit.nickname,
                'visits': spit.visits,
                'thumbup': spit.thumbup,
                'comment': spit.comment,
                # 'share': 111,
                # 'state': 1,
                # 'avatar': #spit.avatar.url,
                'collected': spit.collected,
                'hasthumbup': spit.hasthumbup,
                'parent': None
            }
            data_list.append(spit_dict)

        return JsonResponse({
            'results': data_list,
        })

    def post(self, request):
        user = request.user
        # 1、获取前端数据，json类型
        data_json = request.body.decode()
        # 2、将json数据转化为字典
        data_dict = json.loads(data_json)
        content = data_dict['content']
        try:
            parent_id = data_dict['parent']
            parent = Spit.objects.get(id=parent_id)
            parent_dict = {
                'id': parent.id,
                'content': parent.content,
                'publishtime': parent.publishtime,
                'userid': parent.userid,
                'nickname': parent.nickname,
                'visits': parent.visits,
                'thumbup': parent.thumbup,
                'comment': parent.comment,
                # 'share': 111,
                # 'state': 1,
                # 'avatar': #parent.avatar.url,
                'collected': parent.collected,
                'hasthumbup': parent.hasthumbup,
            }
        except:
            parent = None
            parent_dict = None
        if parent is not None:
            if not user.is_authenticated():
                return JsonResponse({
                    'message': '用户未登录',
                    'error': True
                },status=400)
        spit = Spit.objects.create(
            content=content,
            publishtime=datetime.now(),
            userid=3,  # request.user.id,
            nickname=333,  # request.user.nickname,
            visits=0,
            thumbup=0,
            # share=0,
            # state=0,
            comment=0,
            avatar=333,  # request.user.avatar,
            collected=False,
            hasthumbup=False,
            parent=parent
        )
        if parent is not None:
            parent.comment += 1
            parent.save()

        return JsonResponse({
            'id': spit.id,
            'content': spit.content,
            'publishtime': spit.publishtime,
            'userid': spit.userid,
            'nickname': spit.nickname,
            'visits': spit.visits,
            'thumbup': spit.thumbup,
            'comment': spit.comment,
            # 'share': 111,
            # 'state': 1,
            # 'avatar': #spit.avatar.url,
            'collected': spit.collected,
            'hasthumbup': spit.hasthumbup,
            'parent': parent_dict
        })


class SpitCollectView(View):
    def put(self, request, id):
        user = request.user
        if not user.is_authenticated():
            return JsonResponse({
                'message': '用户未登录',
                'success': False
            },status=400)
        # 获取数据
        spit = Spit.objects.get(id=id)
        # 判断当前收藏状态
        if spit.collected:
            spit.collected = False
        else:
            spit.collected = True

        # 保存
        spit.save()
        # 返回数据
        return JsonResponse({
            'message': 'OK',
            'success': True
        })


class SpitUpdatethumbupView(View):
    def put(self, request, id):
        user = request.user
        if not user.is_authenticated():
            return JsonResponse({
                'message': '用户未登录',
                'success': False
            },status=400)
        # 获取数据
        spit = Spit.objects.get(id=id)
        # 判断当前点赞状态
        if spit.hasthumbup:
            spit.hasthumbup = False
            spit.thumbup -= 1
        else:
            spit.hasthumbup = True
            spit.thumbup += 1
        # 保存
        spit.save()
        # 返回数据
        return JsonResponse({
            'message': 'OK',
            'success': True
        })


class SpitChildrenView(View):
    def get(self, request, id):

        # 获取吐槽id
        spit = Spit.objects.get(id=id)
        # 获取二级吐槽数据
        children_spits = Spit.objects.filter(parent_id=id).order_by('-publishtime')
        data_list = []
        for children_spit in children_spits:
            child_dict = {
                'id': children_spit.id,
                'content': children_spit.content,
                'publishtime': children_spit.publishtime,
                'userid': children_spit.userid,
                'nickname': children_spit.nickname,
                'visits': children_spit.visits,
                'thumbup': children_spit.thumbup,
                'comment': children_spit.comment,
                # 'avatar': #children_spit.avatar.url,
                'collected': children_spit.collected,
                'hasthumbup': children_spit.hasthumbup,
                'parent': {
                    'id': spit.id,
                    'content': spit.content,
                    'publishtime': spit.publishtime,
                    'userid': spit.userid,
                    'nickname': spit.nickname,
                    'visits': spit.visits,
                    'thumbup': spit.thumbup,
                    'comment': spit.comment,
                    # 'avatar': #spit.avatar.url,
                    'collected': spit.collected,
                    'hasthumbup': spit.hasthumbup,
                }
            }
            data_list.append(child_dict)
        return JsonResponse({
            'results': data_list
        })


class SpitDetailView(View):
    def get(self, request, id):
        # 获取吐槽详情页数据
        spit = Spit.objects.get(id=id)
        return JsonResponse({
            'id': spit.id,
            'content': spit.content,
            'publishtime': spit.publishtime,
            'userid': spit.userid,
            'nickname': spit.nickname,
            'visits': spit.visits,
            'thumbup': spit.thumbup,
            'comment': spit.comment,
            # 'avatar': #spit.avatar.url,
            'collected': spit.collected,
            'hasthumbup': spit.hasthumbup,
            'parent': None
        })
